package com.sdhs.paas.authserver.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sdhs.paas.authserver.configuration.KeyConfiguration;
import com.sdhs.paas.authserver.entity.ClientDTO;
import com.sdhs.paas.authserver.service.AuthClientService;
import com.sdhs.paas.authshare.common.msg.ObjectRestResponse;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.logger.Logger;
import com.sdhs.paas.sdk.core.logger.LoggerFactory;
import com.sdhs.paas.sdk.core.web.client.PaasRestTemplate;

/**
 * Created by ace on 2017/9/10.
 */
@RestController
@RequestMapping("client")
public class ClientController {
	private static Logger logger = LoggerFactory.getLogger(ClientController.class);

	// @Autowired
	// PaasRestTemplate paasRestTemplate;
	@Autowired
	private AuthClientService authClientService;
	@Autowired
	private KeyConfiguration keyConfiguration;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/token", method = RequestMethod.POST)
	public ObjectRestResponse<ClientDTO> getAccessToken(@RequestBody ClientDTO client) throws Exception {
		String token = authClientService.apply(client.getClientId(), client.getSecret());
		ClientDTO resClientDTO = new ClientDTO();
		resClientDTO.setToken(token);
		logger.info(token);
		return new ObjectRestResponse<ClientDTO>().data(resClientDTO);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/myClient")
	public ObjectRestResponse<ClientDTO> getAllowedClient(@RequestBody ClientDTO client) {
		 logger.info("serviceId--" + client.getServiceId() + "secret" + client.getSecret());
		List<String> strList = authClientService.getAllowedClient(client.getServiceId(), client.getSecret());
		ClientDTO resClientDTO = new ClientDTO();
		resClientDTO.setAllowClientList(strList);
		return new ObjectRestResponse<ClientDTO>().data(resClientDTO);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/servicePubKey", method = RequestMethod.POST)
	public ObjectRestResponse<ClientDTO> getServicePublicKey(@RequestBody ClientDTO client) throws Exception {
		authClientService.validate(client.getClientId(), client.getSecret());
		byte[] key = keyConfiguration.getServicePubKey();
		ClientDTO resClientDTO = new ClientDTO();
		resClientDTO.setKey(key);
		return new ObjectRestResponse<ClientDTO>().data(resClientDTO);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/userPubKey", method = RequestMethod.POST)
	public ObjectRestResponse<ClientDTO> getUserPublicKey(@RequestBody ClientDTO client) throws Exception {
		authClientService.validate(client.getClientId(), client.getSecret());
		byte[] key = keyConfiguration.getUserPubKey();
		ClientDTO resClientDTO = new ClientDTO();
		resClientDTO.setKey(key);
		return new ObjectRestResponse<ClientDTO>().data(resClientDTO);
	}
	
	
	
	// public PaasBaseResponse<ClientDTO> getAccessToken( @RequestBody
	// PaasBaseRequest<ClientDTO> client ) throws Exception {
	// @RequestMapping(value = "/token", method = RequestMethod.POST)
	// public PaasBaseResponse<ClientDTO> getAccessToken( @RequestBody
	// PaasBaseRequest<ClientDTO> client ) throws Exception {
	// logger.info("clientId--"+client.getBody().getClientId()+"
	// secret"+client.getBody().getSecret());
	// String token = authClientService.apply(client.getBody().getClientId(),
	// client.getBody().getSecret());
	// ClientDTO resClientDTO = new ClientDTO();
	// resClientDTO.setToken(token);
	// return new PaasBaseResponse<>(resClientDTO);
	// }
	//
	// @RequestMapping(value = "/myClient")
	// public PaasBaseResponse<ClientDTO> getAllowedClient(@RequestBody
	// PaasBaseRequest<ClientDTO> client) {
	// logger.info("serviceId--"+client.getBody().getServiceId()+"
	// secret"+client.getBody().getSecret());
	// List<String> strList =
	// authClientService.getAllowedClient(client.getBody().getServiceId(),
	// client.getBody().getSecret());
	// ClientDTO resClientDTO = new ClientDTO();
	// resClientDTO.setAllowClientList(strList);
	// return new PaasBaseResponse<>(resClientDTO);
	// }
	//
	// @RequestMapping(value = "/servicePubKey",method = RequestMethod.POST)
	// public PaasBaseResponse<ClientDTO> getServicePublicKey(@RequestBody
	// PaasBaseRequest<ClientDTO> client) throws Exception {
	// logger.info("clientId--"+client.getBody().getClientId()+"
	// secret"+client.getBody().getSecret());
	// authClientService.validate(client.getBody().getClientId(),
	// client.getBody().getSecret());
	// byte[] key = keyConfiguration.getServicePubKey();
	// ClientDTO resClientDTO = new ClientDTO();
	// resClientDTO.setKey(key);
	// logger.error(new String(key,"utf-8"));
	// return new PaasBaseResponse<>(resClientDTO);
	// }

	// @RequestMapping(value = "/testservicePubKey",method = RequestMethod.POST)
	// public PaasBaseResponse<ClientDTO> gettestServicePublicKey(@RequestBody
	// PaasBaseRequest<ClientDTO> client) throws Exception {
	// //authserver:9977
	// ResponseEntity<PaasBaseResponse<ClientDTO>> result =
	// paasRestTemplate.exchange(
	// "http://authserver/client/servicePubKey", HttpMethod.POST,
	// new HttpEntity<>(new PaasBaseRequest<>(client.getBody())),
	// new ParameterizedTypeReference<PaasBaseResponse<ClientDTO>>() {
	// });
	// byte[] key = result.getBody().getBody().getKey();
	// logger.error(new String(key,"utf-8"));
	// return result.getBody();
	// }

	// @RequestMapping(value = "/userPubKey",method = RequestMethod.POST)
	// public PaasBaseResponse<ClientDTO> getUserPublicKey(@RequestBody
	// PaasBaseRequest<ClientDTO> client) throws Exception {
	// logger.info("clientId--"+client.getBody().getClientId()+"
	// secret"+client.getBody().getSecret());
	// authClientService.validate(client.getBody().getClientId(),
	// client.getBody().getSecret());
	// byte[] key = keyConfiguration.getUserPubKey();
	// ClientDTO resClientDTO = new ClientDTO();
	// resClientDTO.setKey(key);
	// return new PaasBaseResponse<>(resClientDTO);
	// }

}
